//存储灰太狼小灰灰出现的位置
var points = [
	[110, 137],
	[30, 182],
	[115, 216],
	[30, 245],
	[195, 166],
	[210, 236],
	[41, 318],
	[130, 298],
	[219, 319],
];
//存储当前出现的灰太狼小灰灰出现的图片
var index = [];
//获取游戏界面
var gameFace = document.getElementById("wrap");
//分数
var score = document.getElementById("score");
//进度条
var progress = document.getElementById("progress");
//开始
var begin = document.getElementsByClassName("begin")[0];
//开始按钮
var start = document.getElementById("kai1");
//结束
var over = document.getElementsByClassName("over")[0];
//再来一局按钮
var again = document.getElementById("zai1");
//开始按钮点击事件
start.onclick = function() {
		//隐藏掉开始标签
		begin.style.display = 'none';
		//	gameFace.remove(begin);
		//开始游戏
		startGame();
		//开始计时
		changeTime();
	}
//再来一局按钮点击事件
again.onclick = function() {
		//隐藏掉结束标签
		over.style.display = "none";
		//分数清零
		score.innerHTML = 0;
		//开始游戏
		startGame();
		//开始计时
		changeTime();
	}
//随机数
function randomNum() {
	var num = Math.floor(Math.random() * 9);
	return num;
}
//开始游戏
var time1;
function startGame() {
	time1 = setInterval(function(){
		//创建一个img标签,并给其一个随机位置
		var img = creatWolf();
		var name =Math.random()*100 >20?"h":"x";
		//通过计时器完成更换图片的操作
		//记录图片编号
		var imgIndex=0;
		//记录是否应该反向
		var orReverse = false;
		var time2 = setInterval(function(){
				img.src = "img/"+name+imgIndex+".png";
				if (imgIndex>=5) {
					orReverse = true;
				}
				if (orReverse==false) {
					imgIndex++;
				}else{
					imgIndex--;
				}
				//当imgIndex<0时,一次图片轮换结束,此时清除定时器,删除图片
				if (imgIndex<0) {
					clearInterval(time2);
					wrap.removeChild(img);
				}
		},100);
		//对图片添加点击事件
		var orClick =false
		img.onclick=function(){
			//判断被打的是谁
			if (orClick ==false) {
				orClick ==true;
				if (name=='x') {
					score.innerHTML -=10;
				} else{
					score.innerHTML=score.innerHTML-0 + 10;
				}
				//更换被打图片
				clearInterval(time2);
				var hitIndex = 6;
				var time3 = setInterval(function(){
					img.src =  "img/"+name+hitIndex+".png";
					hitIndex++;
					if (hitIndex>9) {
						clearInterval(time3);
						wrap.removeChild(img)
					}
				},100)
			}
		}
	},1000);
}
function creatWolf(){
	var img = document.createElement("img");
	var randomnum = randomNum();
	//获取对应位置数组
	var point = points[randomnum];
	//给图片添加class
	img.className = "wolf";
	//图片位置
	img.style.top = point[1] + "px";
	img.style.left = point[0] + "px";
	wrap.appendChild(img);
	return img;
}
//开始计时
function changeTime(){
	//改变进度条的宽度
	var t = 0;
	var b = 180;
	var c = -180;
	var d = 90;
	var timeTotle = setInterval(function(){
		t++;
		progress.style.width = Tween.Linear(t,b,c,d)+"px";
		if (t==d) {
			clearInterval(timeTotle);
			//时间结束时,over标签出现
			over.style.display="block";
			//停止游戏的继续运行
			clearInterval(time1);
		}
		if (score.innerHTML<0) {
			score.innerHTML=0;
			clearInterval(timeTotle);
			clearInterval(time1);
			over.style.display="block";
		}
	},1000);
}